
#include "mainwindow.h"
#include "service/event/ctkEventAdmin.h"
#include "../../Hello/hello_service.h"
#include "menubar.h"
#include <QMenuBar>
#include <iostream>

MainWindow::MainWindow(ctkPluginContext *context, QWidget *parent)
    : QMainWindow(parent)
    , context_(context)
{
    MenuBar *menubar  = new MenuBar(context, this);
    setMenuBar(menubar);
    // 
    QAction* action = new QAction("发送事件", this);
    menubar->addAction(action);
    connect(action, &QAction::triggered, this, &MainWindow::actionSendEvent);
}

MainWindow::~MainWindow()
{
    std::cout << "~MainWindow" << std::endl;
}

void MainWindow::actionSendEvent()
{
    //获取事件服务接口
    ctkServiceReference ref = context_->getServiceReference<ctkEventAdmin>();
    ctkEventAdmin* eventAdmin{nullptr};
    if(ref)
    {
        eventAdmin = context_->getService<ctkEventAdmin>(ref);
        context_->ungetService(ref);
    }
    //发送事件
    ctkDictionary message;
    ctkDictionary message2;
    ctkDictionary message3;
    message.insert("About", "im a message to about plugin");
    message2.insert("About", "im AAA message to about plugin");
    message3.insert("MMM", "im MMM message to about plugin");
    if(eventAdmin)
        eventAdmin->postEvent(ctkEvent("About", message));
    if(eventAdmin)
        eventAdmin->postEvent(ctkEvent("AAA", message2));
    if(eventAdmin)
        eventAdmin->postEvent(ctkEvent("About", message3));

    ctkServiceReference ref2 =context_->getServiceReference<HelloService>();
    HelloService* hello{nullptr};
    if(ref2)
    {
        hello = context_->getService<HelloService>(ref2);
        context_->ungetService(ref2);
    }
    if(hello)
    {
        hello->sayHello();
    }
}
